# -*- coding: utf-8 -*-
import cv2
import numpy as np

__all__ = [
    'rotate', 'rotate_bound'
]


def rotate(image, angle, center=None, scale=1.0):
    (h, w) = image.shape[:2]

    if center is None:
        center = (w // 2, h // 2)

    m = cv2.getRotationMatrix2D(center, -angle, scale)
    rotated = cv2.warpAffine(image, m, (w, h))

    return rotated


def rotate_bound(image, angle, scale=1.0):
    (h, w) = image.shape[:2]
    (cX, cY) = (w / 2, h / 2)

    m = cv2.getRotationMatrix2D((cX, cY), -angle, scale)
    cos = np.abs(m[0, 0])
    sin = np.abs(m[0, 1])

    nw = int((h * sin) + (w * cos))
    nh = int((h * cos) + (w * sin))

    m[0, 2] += (nw / 2) - cX
    m[1, 2] += (nh / 2) - cY

    return cv2.warpAffine(image, m, (nw, nh))
